package com.northwind.controller;

import com.northwind.entity.User;
import com.northwind.service.UserService;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.stream.Collectors;

@Controller
@RequestMapping("user")
public class UserController {
    @Resource
    UserService userService;

    @ResponseBody
    @RequestMapping("queryAllPage")
    public HashMap<String, Object> queryAllPage(Integer page, Integer rows) {
        //System.out.println("分页查询的数据为:" + userService.queryAllPage(page, rows));
        return userService.queryAllPage(page, rows);
    }

    @ResponseBody
    @RequestMapping("updateStatus")
    public void updateStatus(User user) {
        System.out.println("要进行修改用户的信息:" + user);
        userService.update(user);
    }

    @ResponseBody
    @RequestMapping("edit")
    public String edit(User user, String oper) {
        String id = null;
        if (oper.equals("add")) {
            System.out.println("要添加的用户为:" + user);
            id = userService.add(user);

        }
        if (oper.equals("del")) {
            System.out.println("删除的用户为:" + user);
            userService.delete(user);
        }
        if (oper.equals("edit")) {
            System.out.println("修改的用户为" + user);
            userService.update(user);
        }
        return id;
    }

    @ResponseBody
    @RequestMapping("fileUpload")
    public HashMap<String, Object> fileUpload(MultipartFile headImg, String userId) {
        System.out.println("文件名:" + headImg.getOriginalFilename());
        System.out.println("文件大小:" + headImg.getSize());
        HashMap<String, Object> map = userService.fileUploadByAliyun(headImg, userId);
        return map;
    }

    /**
     * 未完成 202112281202
     */
    @ResponseBody
    @RequestMapping("showEchartsUser")
    public HashMap<String ,Object> showEchartsUser() {
        HashMap<String, Object> map = new HashMap();
        //jan feb mar apr may jun jul aug sept oct nov dec
        String jan = userService.totalByMonth("2021-01%").toString();
        String feb = userService.totalByMonth("2021-02%").toString();
        String mar = userService.totalByMonth("2021-03%").toString();
        String apr = userService.totalByMonth("2021-04%").toString();
        String may = userService.totalByMonth("2021-05%").toString();
        String jun = userService.totalByMonth("2021-06%").toString();
        String jul = userService.totalByMonth("2021-07%").toString();
        String aug = userService.totalByMonth("2021-08%").toString();
        String sept = userService.totalByMonth("2021-09%").toString();
        String oct = userService.totalByMonth("2021-10%").toString();
        String nov = userService.totalByMonth("2021-11%").toString();
        String dec = userService.totalByMonth("2021-12%").toString();

        map.put("countUser", Arrays.asList(jan,feb,mar,apr,may,jun,jul,aug,sept,oct,nov,dec));

        Integer total = userService.total();
        map.put("total",total);
        return map;
    }
}
